import requests
import spider
import urllib
from spider import common

class BinnerSpider():
    def __init__(self):
        self.url = "https://h5.ele.me/restapi/shopping/v2/banners?consumer=1&type=1&latitude=22.54286&longitude=114.059563";

    def run(self):
        res =  requests.get(self.url,headers=spider.headers)
        res_data = res.json()
        #print(res_data)
        self.dataParse(res_data)

    def dataParse(self,data_list):
        all_data = []
        for data in data_list:
            image_hash = data.get("image_hash","")
            imge_url = common.img_handle_elm(image_hash)
            url = data.get("url", "")
            url = url[url.find("&url=") + 5:]
            url = urllib.request.unquote(url)
            result = {
                "img":imge_url,
                "url":url,
                "type":"1"
            }
            all_data.append(result)
        spider.insert_list(all_data,"exihibition")